package com.taotao.inventory.service.imlp;

import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.taotao.common.core.domain.Result;
import com.taotao.common.security.utils.SecurityUtils;
import com.taotao.inventory.domain.PurchaseDetails;
import com.taotao.inventory.domain.PurchaseOrder;
import com.taotao.inventory.domain.vo.OrderVo;
import com.taotao.inventory.domain.vo.PurchaseDemandVo;
import com.taotao.system.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.taotao.inventory.mapper.TSkuInfoMapper;
import com.taotao.inventory.domain.TSkuInfo;
import com.taotao.inventory.service.ITSkuInfoService;

/**
 * 库存预警Service业务层处理
 *
 * @author byf
 * @date 2025-01-14
 */
@Service
public class TSkuInfoServiceImpl implements ITSkuInfoService
{
    @Autowired
    private TSkuInfoMapper tSkuInfoMapper;



    /**
     * 查询库存预警列表
     *
     * @param tSkuInfo 库存预警
     * @return 库存预警
     */
    @Override
    public List<TSkuInfo> selectTSkuInfoList(TSkuInfo tSkuInfo)
    {
        return tSkuInfoMapper.selectTSkuInfoList(tSkuInfo);
    }


    /**
     * 采购需求提交
     * @param skuId
     * @return
     */
    @Override
    public Result Increase(Long skuId) {

        PurchaseDemandVo purchaseDemandVo = new PurchaseDemandVo();
        //skuId
        purchaseDemandVo.setSkuId(skuId);
        //用户ID
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userUserid = loginUser.getUserid();
        purchaseDemandVo.setUserId(userUserid);
        //状态默认0
        purchaseDemandVo.setProcurementStatus(1);
        //查skuId
        List<PurchaseDemandVo> purchaseSkuId = tSkuInfoMapper.purchaseDemand(skuId);
        if (purchaseSkuId.size() == 0){
            tSkuInfoMapper.insertPurchaseDemand(purchaseDemandVo);
        }else {
            return Result.error("该采购单已存在");
        }
        return Result.success(null, "采购需求提交成功");
    }

    /**
     * 查找采购状态为0的采购需求
     * @param purchaseDemandVo
     * @return
     */
    @Override
    public Result purchaseStatus(PurchaseDemandVo purchaseDemandVo) {
        purchaseDemandVo.setProcurementStatus(0);

        List<PurchaseDemandVo> purchaseStatusList = tSkuInfoMapper.purchaseStatus(purchaseDemandVo);

        return Result.success(purchaseStatusList);
    }

    /**
     * 根据供应商查询需求数据
     *
     * @param supplierId
     * @return
     */
    @Override
    public List<PurchaseDemandVo> selectSuppilerById(Long supplierId) {
        List<PurchaseDemandVo> purchaseStatusList = tSkuInfoMapper.selectSuppilerById(supplierId);
        return purchaseStatusList;
    }

    /**
     * 订单添加
     * @param orderVo
     * @return
     */
    @Override
    public Result orderAdd(OrderVo orderVo) {

//        //反序列化
//        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(orderVo));
//        OrderVo vo = jsonObject.toJavaObject(OrderVo.class);

        //获取登录人的ID
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userUserid = loginUser.getUserid();



        //订单添加
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setPurchaseOrderNumber(orderVo.getPurchaseOrderNumber());
        purchaseOrder.setUserId(userUserid);
        purchaseOrder.setSupplierId(orderVo.getSupplierId());
        purchaseOrder.setStatus(0);

        tSkuInfoMapper.orderAdd(purchaseOrder);

        PurchaseDetails purchaseDetails = new PurchaseDetails();

        //获取订单ID
        Long id = purchaseOrder.getId();

        //订单详情添加
        purchaseDetails.setPurchaseId(id);
        purchaseDetails.setSkuId(orderVo.getSkuId());
        purchaseDetails.setNum(orderVo.getNum());

        tSkuInfoMapper.DetailsAdd(purchaseDetails);


        //修改采购需求状态为已采购
        PurchaseDemandVo purchaseDemandVo = new PurchaseDemandVo();

        Long skuId = purchaseDemandVo.getSkuId();
        skuId=Long.valueOf(orderVo.getSkuId());
        tSkuInfoMapper.DemandSkuId(skuId);


        return Result.success(null, "订单添加成功");
    }

    /**
     * 对订单进行审核
     * @param id
     * @return
     */
    @Override
    public Result applyId(Long id) {
        //获取用户ID
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        tSkuInfoMapper.applyId(id,userId);
        return Result.success(null, "订单审核成功");
    }
}
